package controller.webservice;

import net.sf.json.JSONObject;
import util.JsonUtil;
import controller.BidCtrl;
import controller.RoundCtrl;
import controller.SectionStudentCtrl;
import exception.InputValidationErrors;

/**
 * A controller class to process web service requests.
 * 
 * @author jaslin.wee.2012, huangjm.2012, huiwensong.2012, mohameds.2012,
 *         sheryl.tay.2012
 * 
 */

public class WebServiceCtrl {

	private DumpCtrl dumpCtrl = new DumpCtrl();
	private BidCtrl bidCtrl = new BidCtrl();
	private SectionStudentCtrl sectionstudentCtrl = new SectionStudentCtrl();
	private RoundCtrl roundCtrl = new RoundCtrl();

	/**
	 * Processes dump user web service request.
	 * 
	 * @param request
	 *            a JSON request string
	 * @return a JSON Object response
	 */
	public JSONObject processDumpUserRequest(String request) {
		JsonRequestParser r = null;

		/*
		 * Parse the JSON request string.
		 */
		try {
			r = new JsonRequestParser(request, "userid");
		} catch (InputValidationErrors e) {
			return JsonUtil.newJsonErrorResponse(e.toJsonArray());
		}

		// relay to Dump Controller
		return dumpCtrl.dumpUser(r.getRequest().getString("userid"));
	}

	/**
	 * Processes dump bid request.
	 * 
	 * @param request
	 *            a JSON request string
	 * @return a JSON Object response
	 */
	public JSONObject processDumpBidRequest(String request) {
		JSONObject r = null;

		/*
		 * Parse the JSON request string.
		 */
		try {
			r = new JsonRequestParser(request, "course", "section")
					.getRequest();
		} catch (InputValidationErrors e) {
			return JsonUtil.newJsonErrorResponse(e.toJsonArray());
		}

		// relay to Dump Controller
		return dumpCtrl.dumpBid(r.getString("course"), r.getString("section"));
	}

	/**
	 * Processes dump section request.
	 * 
	 * @param request
	 *            a JSON request string
	 * @return a JSON Object response
	 */
	public JSONObject processDumpSectionRequest(String request) {
		JSONObject r = null;

		/*
		 * Parse the JSON request string.
		 */
		try {
			r = new JsonRequestParser(request, "course", "section")
					.getRequest();
		} catch (InputValidationErrors e) {
			return JsonUtil.newJsonErrorResponse(e.toJsonArray());
		}

		// relay to Dump Controller
		return dumpCtrl.dumpSection(r.getString("course"),
				r.getString("section"));
	}

	/**
	 * Processes dump table request.
	 * 
	 * @return a JSON Object response
	 */
	public JSONObject processDumpTableRequest() {
		return dumpCtrl.dumpTable();
	}

	/**
	 * Processes delete bid request.
	 * 
	 * @param request
	 *            a JSON request string
	 * @return a JSON Object response
	 */
	public JSONObject processDeleteBidRequest(String request) {
		JSONObject r = null;

		/*
		 * Parse the JSON request string.
		 */
		try {
			r = new JsonRequestParser(request, "userid", "code", "section")
					.getRequest();
		} catch (InputValidationErrors e) {
			return JsonUtil.newJsonErrorResponse(e.toJsonArray());
		}

		// try to delete bid
		try {
			bidCtrl.deleteBid(r.getString("userid"), r.getString("code"),
					r.getString("section"));

			return JsonUtil.newJsonSuccessResponse();
		} catch (InputValidationErrors e) {
			return JsonUtil.newJsonErrorResponse(e.toJsonArray());
		}

	}

	/**
	 * Processes update bid request.
	 * 
	 * @param request
	 *            a JSON request string
	 * @return a JSON Object response
	 */
	public JSONObject processUpdateBidRequest(String request) {
		JSONObject r = null;

		/*
		 * Parse the JSON request string.
		 */
		try {
			r = new JsonRequestParser(request, "amount", "userid", "code",
					"section").getRequest();
		} catch (InputValidationErrors e) {
			return JsonUtil.newJsonErrorResponse(e.toJsonArray());
		}

		try {
			bidCtrl.updateBid(r.getString("userid"), r.getString("code"),
					r.getString("section"), r.getString("amount"));
			return JsonUtil.newJsonSuccessResponse();
		} catch (InputValidationErrors e) {
			return JsonUtil.newJsonErrorResponse(e.toJsonArray());
		}
	}

	/**
	 * Processes drop section request.
	 * 
	 * @param request
	 *            a JSON request string
	 * @return a JSON Object response
	 */
	public JSONObject processDropSectionRequest(String request) {
		JSONObject r = null;

		/*
		 * Parse the JSON request string.
		 */
		try {
			r = new JsonRequestParser(request, "userid", "code", "section")
					.getRequest();
		} catch (InputValidationErrors e) {
			return JsonUtil.newJsonErrorResponse(e.toJsonArray());
		}

		try {

			sectionstudentCtrl.dropSectionForStudent(r.getString("code"),
					r.getString("section"), r.getString("userid"));

			return JsonUtil.newJsonSuccessResponse();
		} catch (InputValidationErrors e) {
			return JsonUtil.newJsonErrorResponse(e.toJsonArray());
		}

	}

	/**
	 * Processes start round request.
	 * 
	 * @return a JSON Object response
	 */
	public JSONObject processStartRoundRequest() {

		try {
			int roundNo = roundCtrl.startRound();

			JSONObject response = new JSONObject();
			response.put("status", "success");
			response.put("round", roundNo);
			return response;

		} catch (InputValidationErrors e) {
			return JsonUtil.newJsonErrorResponse(e.toJsonArray());
		}

	}

	/**
	 * Processes stop round request.
	 * 
	 * @return a JSON Object response
	 */
	public JSONObject processStopRoundRequest() {
		try {
			roundCtrl.stopRound();
			return JsonUtil.newJsonSuccessResponse();
		} catch (InputValidationErrors e) {
			return JsonUtil.newJsonErrorResponse(e.toJsonArray());
		}
	}

}
